package com.app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.app.admin.ServerHome;
import com.app.bean.UserBean;
import com.app.studentActivity.Shiwu;
import com.app.util.DBHelper;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPStaticUtils;

public class MainActivity extends AppCompatActivity {

    private TextView register;
    private EditText id, password;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        register.setOnClickListener(v ->
                startActivity(new Intent(this, Register.class)));
        id.setText("admin");
        password.setText("123456");

        id.setText("1");
        password.setText("1");
        login.setOnClickListener(v -> {

            String idText = id.getText().toString();
            String passwordText = password.getText().toString();

            // 检查输入是否为空
            if (idText.isEmpty() || passwordText.isEmpty()) {
                Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
                return; // 如果输入为空，直接返回，不执行后续的登录操作
            }

            if (id.getText().toString().equals("admin") && password.getText().toString().equals("123456")) {
                UserBean userBean = new UserBean(
                        9999, "admin", "admin", "123456", null, null, null
                );
                SPStaticUtils.put("user", GsonUtils.toJson(userBean));
                startActivity(new Intent(this, ServerHome.class));
                finish();
            } else {
                //sqlite;
                DBHelper dbHelper = new DBHelper(getApplicationContext());
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String selection = "user_id = ? AND password = ?";
                String[] selectionArgs = {id.getText().toString(), password.getText().toString()};
//                select * from user where user_id = ? AND password = ?
                Cursor cursor = db.query("user", null, selection, selectionArgs, null, null, null);
                if (cursor.moveToFirst()) {
                    int Id = cursor.getInt(cursor.getColumnIndex("id"));
                    String username = cursor.getString(cursor.getColumnIndex("username"));
                    String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
                    String password = cursor.getString(cursor.getColumnIndex("password"));
                    String phone_number = cursor.getString(cursor.getColumnIndex("phone_number"));
                    String id_card = cursor.getString(cursor.getColumnIndex("id_card"));
                    String gender = cursor.getString(cursor.getColumnIndex("gender"));
                    UserBean userBean = new UserBean(
                            Id, username, user_id, password, phone_number, id_card, gender
                    );
                    if (id.getText().toString().substring(0, 1).equals("j")) {
                        SPStaticUtils.put("user", GsonUtils.toJson(userBean));
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(this, TeacherMain.class);
                        intent.putExtra("id", Id);
                        startActivity(intent);
                        finish();
                    } else {
                        SPStaticUtils.put("user", GsonUtils.toJson(userBean));
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(this, MainActivity2.class);
//                        Intent intent = new Intent(this, Shiwu.class);
                        intent.putExtra("id", Id);
                        startActivity(intent);
                        finish();
                    }
                } else {
                    Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
                dbHelper.close();
                cursor.close();
            }
        });
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setVisibility(View.GONE);
        ((TextView) toolbar.getChildAt(1)).setText("登录");
        register = findViewById(R.id.register);
        id = findViewById(R.id.id);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
    }
}